package pl.grabar.clinic.client.web.actions;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import pl.grabar.clinic.core.dao.Dao;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

/**
 * @author db
 */
public abstract class JsonPagedDataAction<T extends Serializable> extends ActionSupport implements Preparable {

	private static final long serialVersionUID = 11234241354231998L;

	private static final Integer DEFAULT_PAGE_SIZE = 20;

	@Autowired
	protected Dao<T> dao = null;

	// Your result List
	private List<T> data;

	// Your Total Pages
	private Integer total = 0;

	// All Record
	private Integer records = 0;

	// get how many rows we want to have into the grid - rowNum attribute in the grid
	private Integer rows = DEFAULT_PAGE_SIZE;

	// Get the requested page. By default grid sets this to 1.
	private Integer page = 1;

	// sorting order - asc or desc
	private String sord = "asc";

	// sorting order - asc or desc
	private String sidx;

	// Search Field
	private String searchField;

	// The Search String
	private String searchString;

	// he Search Operation ['eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en','cn','nc']
	private String searchOper;

	public String execute() throws SQLException {
		records = dao.countAll().intValue();
		data = dao.findAll(page, rows, sidx, sord);
		total = (int) Math.ceil((double) records / (double) rows);
		return SUCCESS;
	}

	public String getJSON() throws Exception {
		return execute();
	}

	/* AKCESORY */

	/** Pobiera wartość pola rows. */
	public Integer getRows() {
		return rows;
	}

	/** Ustawia wartość pola rows. */
	public void setRows(Integer rows) {
		this.rows = rows;
	}

	/** Pobiera wartość pola page. */
	public Integer getPage() {
		return page;
	}

	/** Ustawia wartość pola page. */
	public void setPage(Integer page) {
		this.page = page;
	}

	/** Pobiera wartość pola sord. */
	public String getSord() {
		return sord;
	}

	/** Ustawia wartość pola sord. */
	public void setSord(String sord) {
		this.sord = sord;
	}

	/** Pobiera wartość pola sidx. */
	public String getSidx() {
		return sidx;
	}

	/** Ustawia wartość pola sidx. */
	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	/** Pobiera wartość pola searchField. */
	public String getSearchField() {
		return searchField;
	}

	/** Ustawia wartość pola searchField. */
	public void setSearchField(String searchField) {
		this.searchField = searchField;
	}

	/** Pobiera wartość pola searchString. */
	public String getSearchString() {
		return searchString;
	}

	/** Ustawia wartość pola searchString. */
	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}

	/** Pobiera wartość pola searchOper. */
	public String getSearchOper() {
		return searchOper;
	}

	/** Ustawia wartość pola searchOper. */
	public void setSearchOper(String searchOper) {
		this.searchOper = searchOper;
	}

	/** Pobiera wartość pola total. */
	public Integer getTotal() {
		return total;
	}

	/** Ustawia wartość pola total. */
	public void setTotal(Integer total) {
		this.total = total;
	}

	/** Pobiera wartość pola records. */
	public Integer getRecords() {
		return records;
	}

	/** Ustawia wartość pola records. */
	public void setRecords(Integer records) {
		this.records = records;
	}

	/** Pobiera wartość pola data. */
	public List<T> getData() {
		return data;
	}

	/** Ustawia wartość pola data. */
	public void setData(List<T> data) {
		this.data = data;
	}

}
